local diaoli = fk.CreateSkill{
  name = "hanqing__diaoli",
  attached_skill_name = "hanqing__diaoli_viewas&"
}

Fk:loadTranslationTable{
  ["hanqing__diaoli"] = "调力",
  [":hanqing__diaoli"] = "若你没有“调力”牌，你可以于出牌阶段将牌堆顶四张牌置于武将牌上，视为使用【无中生有】。"..
  "否则，所有角色可以选择其手牌与“调力”牌中四张花色各不同的牌，将这些牌当一张【桃】使用。",

  ["#hanqing__diaoli-ex_nihilo"] = "调力：你可以将牌堆顶四张牌置于武将牌上，视为使用【无中生有】",
  ["#hanqing__diaoli-peach"] = "调力：你可以将手牌与“调力”牌中四张花色各不同的牌当一张【桃】使用",
}

diaoli:addEffect("viewas", {
  anim_type = "support",
  mute_card = false,
  pattern = "ex_nihilo,peach",
  prompt = function (self, player, selected_cards, selected)
    return "#hanqing__diaoli-"..self.interaction.data
  end,
  expand_pile = function (self, player)
    local ids = {}
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if p:hasSkill(diaoli.name) and #p:getPile(diaoli.name) > 0 then
        table.insertTable(ids, p:getPile(diaoli.name))
      end
    end
    return ids
  end,
  interaction = function (self, player)
    local all_names = {}
    if #player:getPile(diaoli.name) == 0 and
      #player:getViewAsCardNames(diaoli.name, {"ex_nihilo"}) > 0 then
      table.insert(all_names, "ex_nihilo")
    end
    if table.find(Fk:currentRoom().alive_players, function (p)
      return p:hasSkill(diaoli.name) and #p:getPile(diaoli.name) > 0
    end) and #player:getViewAsCardNames(diaoli.name, {"peach"}) > 0 then
      table.insert(all_names, "peach")
    end
    if #all_names == 0 then
      return
    elseif #all_names == 1 then
      self.interaction.data = all_names[1]
    else
      return UI.CardNameBox { choices = all_names }
    end
  end,
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    if self.interaction.data == "ex_nihilo" then
      return false
    elseif self.interaction.data == "peach" then
      return table.every(selected, function (id)
        return Fk:getCardById(id):compareSuitWith(Fk:getCardById(to_select), true)
      end) and
      (table.contains(player:getHandlyIds(), to_select) or
      table.find(Fk:currentRoom().alive_players, function (p)
        return p:hasSkill(diaoli.name) and table.contains(p:getPile(diaoli.name), to_select)
      end))
    end
  end,
  view_as = function(self, player, cards)
    if self.interaction.data == nil then return end
    local card = Fk:cloneCard(self.interaction.data)
    if self.interaction.data == "ex_nihilo" then
      if #cards ~= 0 then return end
    elseif self.interaction.data == "peach" then
      if #cards ~= 4 then return end
      card:addSubcards(cards)
    end
    card.skillName = diaoli.name
    return card
  end,
  before_use = function (self, player, use)
    if use.card.name == "ex_nihilo" then
      player:addToPile(diaoli.name, player.room:getNCards(4), true, diaoli.name, player)
    end
  end,
  enabled_at_play = function (self, player)
    if #player:getPile(diaoli.name) == 0 and
      #player:getViewAsCardNames(diaoli.name, {"ex_nihilo"}) > 0 then
      return true
    end
    if table.find(Fk:currentRoom().alive_players, function (p)
      return p:hasSkill(diaoli.name) and #p:getPile(diaoli.name) > 0
    end) and #player:getViewAsCardNames(diaoli.name, {"peach"}) > 0 then
      return true
    end
  end,
  enabled_at_response = function (self, player, response)
    return not response and
      table.find(Fk:currentRoom().alive_players, function (p)
        return p:hasSkill(diaoli.name) and #p:getPile(diaoli.name) > 0
      end) and #player:getViewAsCardNames(diaoli.name, {"peach"}) > 0
  end,
})

diaoli:addAI(nil, "vs_skill")

return diaoli
